import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '抖动动画',
      theme: ThemeData(primaryColor: Colors.blue),
      home: Scaffold(
        appBar: AppBar(
          title: Text('抖动动画'),
          centerTitle: true,
        ),
        body: PersonComponentState(),
      ),
    );
  }
}

class PersonComponentState extends StatelessWidget {
  Widget build(BuildContext context) {
    return PersonComp(title: '点击抖动');
  }
}

class PersonComp extends StatefulWidget {
  PersonComp({
    Key key,
    this.icon,
    this.title,
    this.position,
  }) : super(key: key);
  final Widget icon;
  final String title;
  final double position;
  @override
  _PersonComp createState() => _PersonComp();
}

class _PersonComp extends State<PersonComp>
    with SingleTickerProviderStateMixin {
  Animation<double> animation; //动画对象
  AnimationController controller; //动画控制器

  @override
  initState() {
    super.initState();
    controller = AnimationController(
        duration: const Duration(milliseconds: 150), vsync: this);
    animation = Tween(begin: 0.0, end: 0.1).animate(controller);
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        //监听动画完成状态
        controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //监听动画结束的状态
        controller.forward();
      }
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void longClick() {
    if (controller.isAnimating) {
      controller.stop();
    } else {
      controller.forward();
    }
  }

  void click() {}
  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints.expand(),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
            top: 100.0,
            child: RotationTransition(
              turns: animation,
              child: Container(
                constraints:
                    BoxConstraints.tightFor(width: 100.0, height: 100.0),
                decoration: BoxDecoration(color: Colors.red),
              ),
            ),
          ),
          Positioned(
            bottom: 30.0,
            child: FlatButton(
              color: Colors.orange,
              minWidth: 130.0,
              padding: EdgeInsets.all(20.0),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0)),
              child: Text(
                widget.title,
                style: TextStyle(color: Colors.white),
              ),
              //长按事件
              onLongPress: () {
                longClick();
              },
              //点击事件
              onPressed: () {
                longClick();
              },
            ),
          )
        ],
      ),
    );
  }
}
